﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.Bliss.Domain.User
{
    public class User
    {
        public IEnumerable<com.Bliss.User> GetUsers(int companyId)
        {
            UserDbContext context = new UserDbContext();
            var userList = context.Users.Where(u => u.CompanyID == companyId);
            return userList.AsEnumerable();
        }
        public com.Bliss.User GetUser(int userID)
        {
            UserDbContext context = new UserDbContext();
            var user = context.Users.SingleOrDefault(u => u.ID == userID);
            return user;
        }

        public com.Bliss.User ValidateUserLogin(string email, string password)
        {
            UserDbContext context = new UserDbContext();
            var user = context.Users.SingleOrDefault(u => u.Email == email && u.Password == password);
            if (user != null)
            {
                user.LastLogin = DateTime.Now;
                context.SaveChanges();
            }
            return user;
        }
        public IEnumerable<com.Bliss.User> GetFriends(int userID,int companyID)
        {
            UserDbContext context = new UserDbContext();
            var userList = context.Users.Where(u => u.CompanyID == companyID && u.ID != userID);
            return userList.AsEnumerable();
        }
        public void CreateGroup(int userId, string groupName, string Description)
        {
            UserDbContext context = new UserDbContext();
            com.Bliss.Group group = new Bliss.Group();
            if (context.Groups.FirstOrDefault(g => g.GroupName == groupName) == null)
            {
                group.GroupName = groupName;
                group.Description = Description;
                group.CreatorId = userId;
                group.ImageName = string.Empty;
                context.Groups.Add(group);
                context.SaveChanges();

                //context.
            }
            else
            {
                throw new Exception("Group name already exists.");
            }
        }
        public bool IsUserInRole(int userId, string RoleName)
        {
            if (GetUser(userId).Roles.FirstOrDefault(r => r.RoleName == RoleName) == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
